/*
 * PhoneStatReceiver.java
 * classes : com.hymobile.common.phonestatelistener.PhoneStatReceiver
 * @author 牛帅
 * V 1.0.0
 * Create at 2011-11-22 下午3:19:25
 */
package com.wzz.common.phonestatelistener;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.wzz.common.player.PlayerService;

/**
 * 牛帅
 * 电话的挂起
 * 1. 首先要加入2个permission
 * <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
   <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
 * 在要调用的类首先   private PhoneStatReceiver receiver;
 * 在调用类的onCreate()方法里 实例化  
 *  if (receiver == null) {
      receiver = new PhoneStatReceiver();
    }
        在调用类的onCreate() 最后面调用
  
   TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
   tm.listen(receiver, PhoneStateListener.LISTEN_CALL_STATE);
        这样就可以了,内部类已经处理了 接听状态和其他状态的情况
        还可以把这个类作为内部类来处理 
 */
public class PhoneStatReceiver extends PhoneStateListener {

  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
    // case TelephonyManager.CALL_STATE_RINGING:
    //
    // case TelephonyManager.CALL_STATE_OFFHOOK:
    // 接听状态
      case TelephonyManager.CALL_STATE_IDLE:
        if (PlayerService.getInstance().isPause()) {
          PlayerService.getInstance().restart();
        }
        Log.d("Test", "call !!!");
        break;
      // 其他状态
      default:
        if (PlayerService.getInstance().isPlaying()) {
          Log.d("Test", "call");
          PlayerService.getInstance().pause();
        }
        break;
    }
    super.onCallStateChanged(state, incomingNumber);
  }
}
